<?php
namespace jy_baojfee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	//加权分红
	public function up_redfee($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
        $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $lastdaytime = $daytime-24*3600;
		$isfeelog=pdo_get('jy_baojfee_feelog',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
        if($isfeelog['id']){
            return false;
        }
		//贡献值分红
		if($this->setting['gongrate']){
			$members=$this->getall('member',array('redfee >'=>0,'gxfee >'=>0,'uniacid'=>$_SESSION['uniacid']),array('redfee','gxfee','mid'));
			 $allfee=0;
			 foreach ($members as $mv){
				  $fee= $mv['gxfee']*$this->setting['gongrate'];
				  $fee=$mv['redfee']>$fee?$fee:$mv['redfee'];
				  if($fee>0){
					$allfee +=$fee;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'ordersn'=>'贡献值分红',
						'money'=>$fee,
						'createtime'=>TIMESTAMP,
						'day'=>date('Y-m-d',TIMESTAMP),
						'year'=>date('Y',TIMESTAMP),
						'status'=>1,
						'type'=>7,
					);
					$this->insert('commission',$new_commission);
					$this->model('member')->upredfee($mv['mid'],$fee);
				  }
			 }
			 if($allfee && count($members)){
				$onefee= $allfee/count($members);
			 }else{
				 $onefee=0;
			 }
			 $log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'onefee'=>$onefee,
				'jifee'=>$allfee,
				'num'=>count($members),
			  );
			 $this->add_feelog($log,2);
		}      
        $orderprice=pdo_getsum('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($lastdaytime,$daytime)),'price');
        if($orderprice<=0){
            return false;
        }
        $levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title','red_rate'),'id');
        $default_level=$this->plugin_setting('default_level');
        $levels['0']=array('id'=>0,'title'=>'默认等级','red_rate'=>$default_level['red_rate']);
        $log=array(
            'orderprice'=>$orderprice,
        );
        foreach ($levels as $lv){
            $log['level_id']=$lv['id'];
            if (substr($lv['red_rate'], -1) == '%') {
                $jifee = ($orderprice * substr($lv['red_rate'], 0, -1)) / 100;
            } else {
                $jifee = $lv['red_rate'];
            }
            if(!$jifee){
              continue;
            }
            $members=$this->getall('member',array('redfee >'=>0,'level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('redfee','mid'));
            $log['num']=count($members);
            if(!$log['num']){
                continue;
            }
            $log['jifee']=$jifee;
            $log['onefee']=$jifee/$log['num'];
            if(!$log['onefee']){
                continue;
            }
            foreach($members as $mv){
				$fee=$mv['redfee']>$log['onefee']?$log['onefee']:$mv['redfee'];
				if($fee>0){
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mv['mid'],
							'ordersn'=>'级别分红',
							'money'=>$fee,
							'createtime'=>TIMESTAMP,
							'day'=>date('Y-m-d',TIMESTAMP),
							'year'=>date('Y',TIMESTAMP),
							'status'=>1,
							'type'=>5,
					);
					$this->insert('commission',$new_commission);
					$this->model('member')->upredfee($mv['mid'],$fee);
				}
            }
			$this->add_feelog($log,1);
        }
		return true;
	}
	public function add_feelog($log=array(),$type){
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'level_id'=>$log['level_id'],
			'jifee'=>$log['jifee'],
            'onefee'=>$log['onefee'],
			'num'=>$log['num'],
			'orderprice'=>$log['orderprice'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
	public function up_daystime(){
		$commissions=$this->getall('commission',array('day'=>'','uniacid'=>$_SESSION['uniacid']),array('id','mid','createtime'));
		foreach($commissions as $mv){
			$update=array(
				'day'=>date('Y-m-d',$mv['createtime']),
				'year'=>date('Y',$mv['createtime']),
			);
			$this->update('commission',$update,array('id'=>$mv['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
}